|
1
|
|
|
/* |
|
2
|
|
|
* from-bytes: convert bytes to numbers and strings. |
|
3
|
|
|
* Copyright (c) 2017 Rafael da Silva Rocha. |
|
4
|
|
|
* https://github.com/rochars/byte-data |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
const endianness = require("endianness"); |
|
8
|
|
|
const reader = require("../src/read-bytes.js"); |
|
9
|
|
|
const bitDepths = require("../src/bit-depth.js"); |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Turn a byte buffer into what the bytes represent. |
|
13
|
|
|
* @param {!Array<number>|Uint8Array} buffer An array of bytes. |
|
14
|
|
|
* @param {number} bitDepth The bit depth of the data. |
|
15
|
|
|
* Possible values are 1, 2, 4, 8, 16, 24, 32, 40, 48 or 64. |
|
16
|
|
|
* @param {Object} options The options. They are: |
|
17
|
|
|
* - "signed": If the numbers are signed. Default is false (unsigned). |
|
18
|
|
|
* - "float": True for floating point numbers. Default is false. |
|
19
|
|
|
* This option is available for 16, 32 and 64-bit numbers. |
|
20
|
|
|
* - "base": The base of the input. Default is 10. Can be 2, 10 or 16. |
|
21
|
|
|
* - "char": If the bytes represent a string. Default is false. |
|
22
|
|
|
* - "be": If the values are big endian. Default is false (little endian). |
|
23
|
|
|
* - "single": If it should return a single value instead of an array. |
|
24
|
|
|
* Default is false. |
|
25
|
|
|
* @return {!Array<number>|string} |
|
26
|
|
|
*/ |
|
27
|
|
|
function fromBytes(buffer, bitDepth, options={}) { |
|
28
|
|
|
let base = 10; |
|
29
|
|
|
if ("base" in options) { |
|
30
|
|
|
base = options.base; |
|
31
|
|
|
} |
|
32
|
|
|
if (options.be) { |
|
33
|
|
|
endianness.endianness(buffer, bitDepth / 8); |
|
34
|
|
|
} |
|
35
|
|
|
bytesToInt(buffer, base); |
|
36
|
|
|
let bitReader = getBitReader(bitDepth, options.float, options.char); |
|
37
|
|
|
let values = readBytes(buffer, bitDepth, options.signed, bitReader); |
|
38
|
|
|
if (options.char) { |
|
39
|
|
|
values = values.join(""); |
|
40
|
|
|
} |
|
41
|
|
|
if (options.single) { |
|
42
|
|
|
values = values[0]; |
|
43
|
|
|
} |
|
44
|
|
|
return values; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Turn a array of bytes into an array of what the bytes should represent. |
|
49
|
|
|
* @param {!Array<number>|Uint8Array} bytes An array of bytes. |
|
50
|
|
|
* @param {number} bitDepth The bitDepth. 1, 2, 4, 8, 16, 24, 32, 40, 48, 64. |
|
51
|
|
|
* @param {boolean} isSigned True if the values should be signed. |
|
52
|
|
|
* @param {Function} bitReader The function to read the bytes. |
|
53
|
|
|
* @return {!Array<number>|string} |
|
54
|
|
|
*/ |
|
55
|
|
|
function readBytes(bytes, bitDepth, isSigned, bitReader) { |
|
56
|
|
|
let values = []; |
|
57
|
|
|
let i = 0; |
|
58
|
|
|
let j = 0; |
|
59
|
|
|
let offset = bitDepths.BitDepthOffsets[bitDepth]; |
|
60
|
|
|
let len = bytes.length - (offset -1); |
|
61
|
|
|
let maxBitDepthValue = bitDepths.BitDepthMaxValues[bitDepth]; |
|
62
|
|
|
let signFunction = isSigned ? signed : function(x,y){return x;}; |
|
|
|
|
|
|
63
|
|
|
while (i < len) { |
|
64
|
|
|
values[j] = signFunction(bitReader(bytes, i), maxBitDepthValue); |
|
65
|
|
|
i += offset; |
|
66
|
|
|
j++; |
|
67
|
|
|
} |
|
68
|
|
|
return values; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Return a function to read binary data. |
|
73
|
|
|
* @param {number} bitDepth The bitDepth. 1, 2, 4, 8, 16, 24, 32, 40, 48, 64. |
|
74
|
|
|
* @param {boolean} isFloat True if the values are IEEE floating point numbers. |
|
75
|
|
|
* @param {boolean} isChar True if it is a string. |
|
76
|
|
|
* @return {Function} |
|
77
|
|
|
*/ |
|
78
|
|
|
function getBitReader(bitDepth, isFloat, isChar) { |
|
79
|
|
|
let bitReader; |
|
80
|
|
|
if (isChar) { |
|
81
|
|
|
bitReader = reader.readChar; |
|
82
|
|
|
} else { |
|
83
|
|
|
bitReader = reader[getReaderFunctionName(bitDepth, isFloat)]; |
|
84
|
|
|
} |
|
85
|
|
|
return bitReader; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Build a bit reading function name based on the arguments. |
|
90
|
|
|
* @param {number} bitDepth The bitDepth. 1, 2, 4, 8, 16, 24, 32, 40, 48, 64. |
|
91
|
|
|
* @param {boolean} isFloat True if the values are IEEE floating point numbers. |
|
92
|
|
|
* @return {string} |
|
93
|
|
|
*/ |
|
94
|
|
|
function getReaderFunctionName(bitDepth, isFloat) { |
|
95
|
|
|
return 'read' + |
|
96
|
|
|
((bitDepth == 2 || bitDepth == 4) ? 8 : bitDepth) + |
|
97
|
|
|
'Bit' + |
|
98
|
|
|
(isFloat ? "Float" : ""); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Turn bytes to base 10. |
|
103
|
|
|
* @param {!Array<number>|Uint8Array} bytes The bytes as binary or hex strings. |
|
104
|
|
|
* @param {number} base The base. |
|
105
|
|
|
*/ |
|
106
|
|
|
function bytesToInt(bytes, base) { |
|
107
|
|
|
if (base != 10) { |
|
108
|
|
|
let i = 0; |
|
109
|
|
|
let len = bytes.length; |
|
110
|
|
|
while(i < len) { |
|
111
|
|
|
bytes[i] = parseInt(bytes[i], base); |
|
112
|
|
|
i++; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Turn a unsigned number to a signed number. |
|
119
|
|
|
* @param {number} number The number. |
|
120
|
|
|
* @param {number} maxValue The max range for the number bit depth. |
|
121
|
|
|
*/ |
|
122
|
|
|
function signed(number, maxValue) { |
|
123
|
|
|
if (number > parseInt(maxValue / 2, 10) - 1) { |
|
124
|
|
|
number -= maxValue; |
|
125
|
|
|
} |
|
126
|
|
|
return number; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
module.exports.fromBytes = fromBytes; |
|
130
|
|
|
|
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.